package com.cloudxingkong.dianyinke.NewTeacher.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.bean.TeacherMsg;
import com.cloudxingkong.dianyinke.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 云星空科技 on 2016/3/19.
 */
public class MMAdpater extends BaseAdapter {
    private List<TeacherMsg.ListBean> datas;
    private Context context;

    public MMAdpater(List<TeacherMsg.ListBean> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView=View.inflate(context, R.layout.item_mm_listview,null);
            holder=new ViewHolder();
            holder.studentName= (TextView) convertView.findViewById(R.id.mm_student_name);
            holder.mmAvatar= (CircleImageView) convertView.findViewById(R.id.mm_touxiang);
            holder.date= (TextView) convertView.findViewById(R.id.mm_date);
            holder.priContent= (TextView) convertView.findViewById(R.id.mm_content);
            holder.mmZan = (ImageView) convertView.findViewById(R.id.mm_zan);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        holder.studentName.setText(datas.get(position).getMemname());
        holder.date.setText(datas.get(position).getTs().substring(0,19));

        /*if (position % 2 == 0) {
            holder.mmCover.setVisibility(View.GONE);
            holder.norContent.setVisibility(View.VISIBLE);
            holder.norContent.setText(datas.get(position).getText());
        }else{
            holder.norContent.setVisibility(View.GONE);
            holder.mmCover.setVisibility(View.VISIBLE);
            String pics = datas.get(position).getPics();

            if (!pics.equals("")&&pics != null) {
                String[] split = pics.split(",");
                if (!pics.equals("")){
                    if (pics.indexOf(",")==-1) {
                        split[0]=pics;
                        ImageLoader.getInstance().displayImage(split[0],holder.mmCover);
                    }else{
                        ImageLoader.getInstance().displayImage(split[0],holder.mmCover);
                    }
                }
            }

        }*/

        String type = datas.get(position).getType();
        if (type.equals("like")) {
            holder.mmZan.setVisibility(View.VISIBLE);
            holder.priContent.setVisibility(View.GONE);
        }else if (type.equals("msg")) {
            holder.mmZan.setVisibility(View.GONE);
            holder.priContent.setVisibility(View.VISIBLE);
            holder.priContent.setText(datas.get(position).getMsg());
        }

        if (datas.get(position).getAvatar() != null&&!datas.get(position).getAvatar().equals("")) {
            String avatar = datas.get(position).getAvatar();
            com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(avatar,holder.mmAvatar);
        }

        return convertView;
    }
    private class ViewHolder{
        TextView studentName,priContent,date;
        ImageView mmZan;
        CircleImageView mmAvatar;
    }
}
